package oracle.apps.financials.collection.fragment;

import oracle.apps.financials.collection.adapter.DSOAdapter;
import oracle.apps.financials.collection.po.Customer;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ToggleButton;

import com.cyrilmottier.android.greendroid.R;

public class DSOGraphFragment extends BaseGraphFragment {

	protected DSOAdapter adapter;
	private ToggleButton mDSOForAllBn = null;
	private ToggleButton mDSOForPriorPeriodBn = null;
	private ToggleButton mDSOBestPossibleBn = null;
	private Button mDSOParametersBn = null;
	private ToggleButton mDSONoOfCollectorsBn = null;
	
	public DSOGraphFragment() {

	}

	public DSOGraphFragment(Customer customer) {
		super();
		this.customer = customer;
	}


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.dso_graph, container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);		
		ViewGroup graphViewGroup= (ViewGroup)getActivity().findViewById(R.id.graph_view);
		adapter = new DSOAdapter(getActivity(), this.customer,graphViewGroup);
		processGraphViewButtonEvent();	
	}



	public void refreshGraphView(Customer customer) {
		this.customer = customer;
		if(this.adapter != null){
			this.adapter.refreshData(customer);
		}

	}

	public void processGraphViewButtonEvent() {
		mDSOForAllBn = (ToggleButton) getActivity().findViewById(R.id.dso_for_all_button);
		mDSOForPriorPeriodBn = (ToggleButton)getActivity().findViewById(R.id.dso_prior_period_button);
		mDSOBestPossibleBn = (ToggleButton)getActivity().findViewById(R.id.dso_best_possible_button);
		mDSONoOfCollectorsBn = (ToggleButton)getActivity().findViewById(R.id.dso_no_of_collectors_button);
		mDSOParametersBn = (Button)getActivity().findViewById(R.id.add_parameter_button);	
		
		OnClickListener DSOForAllButtonClickedListener = new OnClickListener(){
			public void onClick(View v) {
				if(((ToggleButton)v).isChecked()){
					adapter.setShowDSOForAllGraph(true);
				}
				else{
					adapter.setShowDSOForAllGraph(false);
				}
			}
		}; 
		OnClickListener DSOForPriorPeriodClickedListener = new OnClickListener(){
			public void onClick(View v) {
				if(((ToggleButton)v).isChecked()){
					adapter.setShowDSOForPriorPeriodGraph(true);
				}
				else{
					adapter.setShowDSOForPriorPeriodGraph(false);
				}
			}
		}; 
		OnClickListener DSOBestPossibleClickedListener = new OnClickListener(){
			public void onClick(View v) {
				if(((ToggleButton)v).isChecked()){
					adapter.setShowDSOBestPossibleGraph(true);
				}
				else{
					adapter.setShowDSOBestPossibleGraph(false);
				}
			}
		};
		
		OnClickListener DSONoOfCollectorClickedListner = new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(((ToggleButton)v).isChecked()){
					adapter.setShowNoOfCollectorsGraph(true);
				}
				else{
					adapter.setShowNoOfCollectorsGraph(false);
				}
			}
		};
		
		mDSOForAllBn.setOnClickListener(DSOForAllButtonClickedListener);
		mDSOForPriorPeriodBn.setOnClickListener(DSOForPriorPeriodClickedListener);
		mDSOBestPossibleBn.setOnClickListener(DSOBestPossibleClickedListener);
		mDSONoOfCollectorsBn.setOnClickListener(DSONoOfCollectorClickedListner);
		
	}
	
	@Override
	public void onResume() {
		super.onResume();
		if(mDSOForAllBn != null){
			mDSOForAllBn.setChecked(false);
		}
		if(mDSOForPriorPeriodBn != null){
			mDSOForPriorPeriodBn.setChecked(false);
		};
		if(mDSOBestPossibleBn != null){
			mDSOBestPossibleBn.setChecked(false);			
		};
		if(mDSONoOfCollectorsBn != null){
			mDSONoOfCollectorsBn.setChecked(false);
		}
	}

}
